package top.banner.es;

import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.unit.Fuzziness;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.RangeQueryBuilder;
import org.elasticsearch.index.query.TermQueryBuilder;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder;
import org.elasticsearch.search.aggregations.metrics.MaxAggregationBuilder;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.elasticsearch.search.sort.SortOrder;

import java.io.IOException;

/**
 * @author XGL
 */
public class DocQuery {

    public static void main(String[] args) throws IOException {
        final RestHighLevelClient client = ESClient.init();

        final SearchRequest request = new SearchRequest();
        request.indices("user");

//        request.source(matchAllQuery());
//        request.source(termQuery());
//        request.source(pageQuery());
//        request.source(sortQuery());
//        request.source(fetchQuery());
        request.source(termsQuery());
//        request.source(aggregationQuery());
//        request.source(highlight());
//        request.source(fuzzyQuery());
//        request.source(rangeQuery());
//        request.source(combinationQuery());

        final SearchResponse response = client.search(request, RequestOptions.DEFAULT);

        final SearchHits hits = response.getHits();
        System.out.println(response.getTook());
        System.out.println(hits.getTotalHits());
        for (SearchHit hit : hits) {
            System.out.println(hit.getSourceAsString());
        }


        client.close();
    }


    /**
     * 全部查询
     */
    public static SearchSourceBuilder matchAllQuery() {
        return new SearchSourceBuilder().query(QueryBuilders.matchAllQuery());
    }

    /**
     * 条件查询
     */
    public static SearchSourceBuilder termQuery() {
        return new SearchSourceBuilder().query(QueryBuilders.termQuery("age", 30));
    }

    /**
     * 分页
     */
    public static SearchSourceBuilder pageQuery() {
        final SearchSourceBuilder builder = new SearchSourceBuilder().query(QueryBuilders.matchAllQuery());
        builder.from(0);
        builder.size(2);
        return builder;
    }

    /**
     * 排序
     */
    public static SearchSourceBuilder sortQuery() {
        final SearchSourceBuilder builder = new SearchSourceBuilder().query(QueryBuilders.matchAllQuery());
        builder.sort("age", SortOrder.DESC);
        return builder;
    }

    /**
     * 过滤返回字段
     */
    public static SearchSourceBuilder fetchQuery() {
        final SearchSourceBuilder builder = new SearchSourceBuilder().query(QueryBuilders.matchAllQuery());
        String[] excludes = {};
        String[] includes = {"name"};
        builder.fetchSource(includes, excludes);
        return builder;
    }

    /**
     * 组合查询
     */
    public static SearchSourceBuilder combinationQuery() {
        final SearchSourceBuilder builder = new SearchSourceBuilder();
        final BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
//        boolQueryBuilder.must(QueryBuilders.matchQuery("age", 30));
//        boolQueryBuilder.mustNot(QueryBuilders.matchQuery("sex", "男"));
        boolQueryBuilder.should(QueryBuilders.matchQuery("age", 30));
        boolQueryBuilder.should(QueryBuilders.matchQuery("age", 40));
        builder.query(boolQueryBuilder);
        return builder;
    }

    /**
     * 范围查询
     */
    public static SearchSourceBuilder rangeQuery() {
        final SearchSourceBuilder builder = new SearchSourceBuilder();
        final RangeQueryBuilder rangeQuery = QueryBuilders.rangeQuery("age");
        rangeQuery.gte(30);
        rangeQuery.lt(50);
        builder.query(rangeQuery);

        return builder;
    }

    /**
     * 模糊查询
     */
    public static SearchSourceBuilder fuzzyQuery() {
        final SearchSourceBuilder builder = new SearchSourceBuilder();
        builder.query(QueryBuilders.fuzzyQuery("name", "wangwu").fuzziness(Fuzziness.ONE));

        return builder;
    }

    /**
     * 高亮显示
     */
    public static SearchSourceBuilder highlight() {
        final SearchSourceBuilder builder = new SearchSourceBuilder();
        final TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery("name", "zhangshan");
        final HighlightBuilder highlightBuilder = new HighlightBuilder()
                .preTags("<font color='red'>")
                .postTags("</font>")
                .field("name");

        builder.highlighter(highlightBuilder);

        builder.query(termQueryBuilder);
        return builder;
    }

    /**
     * 聚合查询
     */
    public static SearchSourceBuilder aggregationQuery() {
        final SearchSourceBuilder builder = new SearchSourceBuilder();
        final MaxAggregationBuilder aggregationBuilder = AggregationBuilders.max("maxAge").field("age");
        builder.aggregation(aggregationBuilder);

        return builder;
    }


    /**
     * 分组查询
     */
    public static SearchSourceBuilder termsQuery() {
        final SearchSourceBuilder builder = new SearchSourceBuilder();
        final TermsAggregationBuilder aggregationBuilder = AggregationBuilders.terms("ageGroup").field("age");
        builder.aggregation(aggregationBuilder);

        return builder;
    }
}
